package action

import (
	"errors"
	"github.com/astaxie/beego/orm"
	_ "github.com/astaxie/beego/session"
	. "teamcoop/models"
)

func DynamicAddTodo(sid string, tid int64) (err error) {
	o := orm.NewOrm()
	todo := &Todo{Id: tid}
	err = o.Read(todo)
	//有错，返回nil
	if err != nil {
		err = errors.New("查询TODO失败!")
		return
	}
	dynamic := &Dynamic{
		Type:   TYPE_TODO,
		TodoId: todo.Id,
	}
	_, err = o.Insert(dynamic)
	if err != nil {
		err = errors.New("添加动态失败!")
		return
	}
	return
}

func DynamicAddComment(sid string, cid int64) (err error) {
	o := orm.NewOrm()
	comment := &Comment{Id: cid}
	err = o.Read(comment)
	//有错，返回nil
	if err != nil {
		err = errors.New("查询评论失败!")
		return
	}
	dynamic := &Dynamic{
		Type:      TYPE_COMMENT,
		CommentId: comment.Id,
	}
	_, err = o.Insert(dynamic)
	if err != nil {
		err = errors.New("添加动态失败!")
		return
	}
	return
}
func DynamicList(sid string) (ret_list []*Dynamic, err error) {
	o := orm.NewOrm()
	qs := o.QueryTable("dynamic")
	_, err = qs.All(&ret_list)
	if err != nil {
		err = errors.New("查询动态失败!")
		return
	}
	return
}
